Page({
  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    no: '',
    pwd: '',
    noinput: false,
    pwdinput: false,
  },
  noinput: function (e) {
    this.setData({ no: e.detail.value });
    this.setData({ noinput: true });
    if (this.data.noinput == true && this.data.pwdinput == true) {
      this.setData({ disabled: false });
    }

  },
  pwdinput: function (e) {
    this.setData({ pwd: e.detail.value });
    this.setData({ pwdinput: true });
    if (this.data.noinput == true && this.data.pwdinput == true) {
      this.setData({ disabled: false });
    }
  },

  formSubmit: function (e) {
    
    console.log(e);
    // this.setData({ disabled: true });

    // 不交互版=====

    /*   
      let userInfo = wx.getStorageSync('userInfo');
      userInfo.address[0] = "金科院北05";
      userInfo.token = "lkajsdl;fkjasdf";
      userInfo.type = 0;
      userInfo.userId = 2;
      userInfo.userName = "王瀚";

      wx.setStorageSync('userInfo', userInfo);

      wx.showLoading({
        title: '登录中...',
      })

      wx.switchTab({
        url: '/pages/person/person'
      })

      wx.showToast({
        title: "已登录",
        icon: 'success',
        duration: 1000,
        success: function () {
          
        }
      }) */

    // =====不交互版

    // 交互版

    wx.request({
      url: "http://localhost:9090/sso/userLogin", //仅为示例，并非真实的接口地址
      data: {
        username: e.detail.value.no,
        password: e.detail.value.pwd
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {

        // console.log(res);
        let result = res.data
        if (res.data.status="200") {
          if (res.data.error == true) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          } else {
            console.log(res.data);

            // 写入缓存
            let userInfo = wx.getStorageSync('userInfo');
            userInfo.address[0] = res.data.data.adress;
            userInfo.token = res.data.data.token;
            userInfo.type = res.data.data.type;
            userInfo.userId = res.data.data.userid;
            userInfo.userName = res.data.data.username;

            wx.setStorageSync('userInfo', userInfo);
            
            wx.showLoading({
              title: '登录中...',
            })

            wx.switchTab({
                url: '/pages/person/person'
              })

            wx.showToast({
              title: res.data.msg,
              icon: 'success',
              duration: 2000,
            })

            // setTimeout(function () {
            //   wx.switchTab({
            //     url: '../my/index',
            //   })
            // }, 2000)
          }
        } else {
          wx.showToast({
            title: '服务器出现错误',
            icon: 'none',
            duration: 2000
          })
        }


      }
    })
   

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({ disabled: false });
    var userData = wx.getStorageSync('userData');
    console.log(userData);
    if (typeof (userData) == 'object' && userData.type != '' && userData.name != '') {
      wx.switchTab({
        url: '../login/login',
      }) 
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.no == '' || this.data.pwd == '') {
      this.setData({ disabled: true });
    } else {
      this.setData({ disabled: false });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
